/**
 *  This class is the main class of the "World of Zuul" application. 
 *  "World of Zuul" is a very simple, text based adventure game.  Users 
 *  can walk around some scenery. That's all. It should really be extended 
 *  to make it more interesting!
 * 
 *  To play this game, create an instance of this class and call the "play"
 *  method.
 * 
 *  This main class creates and initialises all the others: it creates all
 *  rooms, creates the parser and starts the game.  It also evaluates and
 *  executes the commands that the parser returns.
 * 
 * @author  Michael Kölling and David J. Barnes
 * @version 2016.02.29
 */

public class Game 
{
    private Parser parser;
    private Room currentRoom;
    private Player player;   
    /**
     * Create the game and initialise its internal map.
     */
    public Game() 
    {
        createRooms();
        parser = new Parser();
        player = new Player();
    }

    /**
     * Create all the rooms and link their exits together.
     */
    private void createRooms()
    {
        Room afood, quick, wo, end, die;
      
        // create the rooms
        afood = new Room("你来到了大胃王比赛入口");
        afood.setFood1(new Food("饺子",20));
        afood.setFood2(new Food("馒头",15));
        quick = new Room("你进入到了一个快餐厅。");
        quick.setFood1(new Food("牛排",30));
        quick.setFood2(new Food("面包",15));
        die = new Room("这是个死胡同，快回去吧！");
        wo = new Room("你进入了一个饮品间。");
        wo.setFood1(new Food("肥宅快乐水",25));
        end = new Room("恭喜你走到了终点，欢迎你冠军！");
        // initialise room exits
        afood.setExit("下边", quick);
        //theater.setExit(null, null, null, outside);
        quick.setExit("左边", die);
        quick.setExit("右边", wo);
        quick.setExit("上边", afood);
        //lab.setExits(outside, office, null, null);
        wo.setExit("左边", quick);
        wo.setExit("上边",end);
        
        die.setExit("右边", quick);
        
        currentRoom = afood;  // start game outside
    }

    /**
     *  Main play routine.  Loops until end of play.
     */
    public void play() 
    {            
        printWelcome();

        // Enter the main command loop.  Here we repeatedly read commands and
        // execute them until the game is over.
                
        boolean finished = false;
        while (! finished) {
            Command command = parser.getCommand();
            finished = processCommand(command);
        }
        System.out.println("真可惜你的饱腹值变成了0，你饿死了。");
    }

    /**
     * Print out the opening message for the player.
     */
    private void printWelcome()
    {
        System.out.println();
        System.out.println("欢迎你来参加大胃王比赛");
        System.out.println("这是一款挑战自己的游戏。");
        System.out.println("你需要边吃边找到终点以获得胜利。");
        System.out.println("每进入一个房间，你都会扣除10饱腹值来换取进入的资格。");
        System.out.println("每一个房间中都会有食物来提高你的饱腹值以保证饱腹值不为0。");
        System.out.println("祝你找到最后的出口成为最后的赢家！");
        System.out.println();
        currentRoom.getDescription();

        player.check();
        currentRoom.printExits();
    }

    /**
     * Given a command, process (that is: execute) the command.
     * @param command The command to be processed.
     * @return true If the command ends the game, false otherwise.
     */
    private boolean processCommand(Command command) 
    {
        boolean wantToQuit = false;

        if(command.isUnknown()) {
            System.out.println("你这是什么操作");
            return false;
        }

        String commandWord = command.getCommandWord();
        if (commandWord.equals("帮助")) {
            printHelp();
        }
        else if (commandWord.equals("往")) {
            player.walk();
            if(!player.isAlive()){
                System.out.println("您的饱腹值为0了，游戏结束!");
                return true;
            }
            goRoom(command);
        }
        else if (commandWord.equals("吃")){
            take(command);
        }    
        return wantToQuit;
    }
    
    private void take(Command command){
        String direction = command.getSecondWord();
        player.take(currentRoom.getFood(direction));
    }    
    // implementations of user commands:

    /**
     * Print out some help information.
     * Here we print some stupid, cryptic message and a list of the 
     * command words.
     */
    private void printHelp() 
    {
        System.out.println("你走岔迷路了，没人来帮助你。");
        System.out.println("出口就在附近了，快快快！");
        System.out.println();
        CommandWords.printCommands();
    }

    /** 
     * Try to go in one direction. If there is an exit, enter
     * the new room, otherwise print an error message.
     */
    private void goRoom(Command command) 
    {
        if(!command.hasSecondWord()) {
            // if there is no second word, we don't know where to go...
            System.out.println("去哪呢?");
            return;
        }

        String direction = command.getSecondWord();
        Room nextRoom = currentRoom.goNext(direction);
        
        currentRoom = nextRoom;
        currentRoom.getDescription();
        player.check();
        currentRoom.printExits();
    }
}
